package com.jetwinner.webfast.module.bigapp.dao.impl;

import com.jetwinner.util.EasyStringUtil;
import com.jetwinner.util.FastHashMap;
import com.jetwinner.util.ListUtil;
import com.jetwinner.webfast.dao.support.DynamicQueryBuilder;
import com.jetwinner.webfast.dao.support.FastJdbcDaoSupport;
import com.jetwinner.webfast.kernel.AppUser;
import com.jetwinner.webfast.kernel.dao.support.OrderBy;
import com.jetwinner.webfast.kernel.exception.RuntimeGoingException;
import com.jetwinner.webfast.module.bigapp.dao.AppGroupDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xulixin
 */
@Repository
public class AppGroupDaoImpl extends FastJdbcDaoSupport implements AppGroupDao {

    private static final String TABLE_NAME = "big_app_group";

    @Override
    public int searchGroupsCount(Map<String, Object> conditions) {
        DynamicQueryBuilder builder = this.createGroupSearchBuilder(conditions)
                .select("count(id)");
        return getNamedParameterJdbcTemplate().queryForObject(builder.getSQL(), conditions, Integer.class);
    }

    @Override
    public List<Map<String, Object>> searchGroups(Map<String, Object> conditions, OrderBy orderBy,
                                                  Integer start, Integer limit) {


        DynamicQueryBuilder builder = createGroupSearchBuilder(conditions)
                .select("*")
                .setFirstResult(start)
                .setMaxResults(limit)
                .orderBy(orderBy);

        return getNamedParameterJdbcTemplate().queryForList(builder.getSQL(), conditions);
    }

    @Override
    public int addGroup(Map<String, Object> group) {
        int groupId = insertMapReturnKey(TABLE_NAME, group).intValue();
        group.put("id", groupId);
        if (groupId <= 0) {
            throw new RuntimeGoingException("Insert Group error.");
        }
        return groupId;
    }

    @Override
    public Map<String, Object> getGroup(Integer id) {
        String sql = String.format("SELECT * FROM %s where id=? LIMIT 1", TABLE_NAME);
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql, id);
        return list != null && list.size() > 0 ? list.get(0) : FastHashMap.build().toMap();
    }

    @Override
    public void updateGroup(Integer id, Map<String, Object> fields) {
        updateMap(TABLE_NAME, fields, "id", id);
    }

    @Override
    public List<Map<String, Object>> getGroupsByIds(Set<Object> ids) {
        String marks = ids.stream().map(v -> "?").collect(Collectors.joining(","));
        String sql = String.format("SELECT * FROM %s WHERE id IN (%s)", TABLE_NAME, marks);
        return getJdbcTemplate().queryForList(sql, ids.toArray());
    }

    private DynamicQueryBuilder createGroupSearchBuilder(Map<String, Object> conditions) {
        if (EasyStringUtil.isNotBlank(conditions.get("title"))) {
            conditions.put("title", "%" + conditions.get("title") + "%");
        }

        return new DynamicQueryBuilder(conditions)
                .from(TABLE_NAME)
                .andWhere("ownerId = :ownerId")
                .andWhere("status = :status")
                .andWhere("title like :title");
    }
}
